define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        month_index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basis/workday/month_index' + location.search,
                    add_url: 'basis/workday/add',
                    // edit_url: 'basis/workday/edit',
                    // del_url: 'basis/workday/del',
                    month_index_url: 'basis/workday/index',
                    multi_url: 'basis/workday/multi',
                    import_url: 'basis/workday/import',
                    table: 'basis_workday',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'year', title: __('Year'), operate: 'LIKE'},
                        {field: 'month', title: __('Month'), operate: 'LIKE'},
                        // {field: 'date', title: __('Date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'is_workday', title: __('是否为工作日'), formatter: Table.api.formatter.toggle, searchList: {1: __('是'), 0: __('否')}},
                        // {field: 'is_tdday', title: __('是否为教研日'), formatter: Table.api.formatter.toggle, searchList: {1: __('是'), 0: __('否')}},
                        // {field: 'is_workday', title: __('Is_workday')},
                        // {field: 'is_tdday', title: __('Is_tdday')},
                        // {field: 'week', title: __('Week')},

                         {field: 'operate', title: __('Operate'),
                             buttons: [
                                 {
                                     name: 'month_index',
                                     text: '查看工作日',
                                     icon: '',
                                     extend: 'data-area=\'["90%", "90%"]\'',
                                     classname: 'btn btn-xs btn-primary btn-dialog',
                                     url: 'basis/workday/index',
                                 }],
                             table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("change", ".text-weigh", function () {
                $(this).data("params", {week: $(this).val()});
                Table.api.multi('', [$(this).data("id")], table, this);
                return false;
            });
        },

        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basis/workday/index' + location.search,
                    // add_url: 'basis/workday/add',
                    edit_url: 'basis/workday/edit',
                    del_url: 'basis/workday/del',
                    multi_url: 'basis/workday/multi',
                    import_url: 'basis/workday/import',
                    table: 'basis_workday',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'year', title: __('Year'), operate: 'LIKE'},
                        // {field: 'month', title: __('Month'), operate: 'LIKE'},
                        {field: 'date', title: __('Date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'is_workday', title: __('是否为工作日'), formatter: Table.api.formatter.toggle, searchList: {1: __('是'), 0: __('否')}},
                        {field: 'is_tdday', title: __('是否为教研日'), formatter: Table.api.formatter.toggle, searchList: {1: __('是'), 0: __('否')}},
                        // {field: 'is_workday', title: __('Is_workday')},
                        // {field: 'is_tdday', title: __('Is_tdday')},
                        // {field: 'week', title: __('Week')},
                        {
                            field: 'week',
                            title: __('周（排班）'),
                            formatter: function (value, row, index) {
                                if(value==1){
                                    return '<select id="c-type"  class="form-control selectpicker text-weigh" data-id="' + row.id + '"  data-rule="required" name="row[type]">' +
                                        '<option value="0"  >请选择</option>' +
                                        '<option value="1" selected >星期一</option>' +
                                        '<option value="2" >星期二</option>' +
                                        '<option value="3" >星期三</option>' +
                                        '<option value="4" >星期四</option>' +
                                        '<option value="5" >星期五</option>' +
                                        '</select>';
                                }else if(value==2){
                                    return '<select id="c-type"  class="form-control selectpicker text-weigh" data-id="' + row.id + '"  data-rule="required" name="row[type]">' +
                                        '<option value="0"  >请选择</option>' +
                                        '<option value="1"  >星期一</option>' +
                                        '<option value="2" selected >星期二</option>' +
                                        '<option value="3" >星期三</option>' +
                                        '<option value="4" >星期四</option>' +
                                        '<option value="5" >星期五</option>' +
                                        '</select>';
                                }else if(value==3){
                                    return '<select id="c-type"  class="form-control selectpicker text-weigh" data-id="' + row.id + '"  data-rule="required" name="row[type]">' +
                                        '<option value="0"  >请选择</option>' +
                                        '<option value="1"  >星期一</option>' +
                                        '<option value="2"  >星期二</option>' +
                                        '<option value="3" selected>星期三</option>' +
                                        '<option value="4" >星期四</option>' +
                                        '<option value="5" >星期五</option>' +
                                        '</select>';
                                }else if(value==4){
                                    return '<select id="c-type"  class="form-control selectpicker text-weigh" data-id="' + row.id + '"  data-rule="required" name="row[type]">' +
                                        '<option value="0"  >请选择</option>' +
                                        '<option value="1"  >星期一</option>' +
                                        '<option value="2"  >星期二</option>' +
                                        '<option value="3" >星期三</option>' +
                                        '<option value="4" selected >星期四</option>' +
                                        '<option value="5" >星期五</option>' +
                                        '</select>';
                                }else if(value==5){
                                    return '<select id="c-type"  class="form-control selectpicker text-weigh" data-id="' + row.id + '"  data-rule="required" name="row[type]">' +
                                        '<option value="0"  >请选择</option>' +
                                        '<option value="1"  >星期一</option>' +
                                        '<option value="2"  >星期二</option>' +
                                        '<option value="3" >星期三</option>' +
                                        '<option value="4" >星期四</option>' +
                                        '<option value="5" selected>星期五</option>' +
                                        '</select>';
                                }else{
                                    return '<select id="c-type"  class="form-control selectpicker text-weigh" data-id="' + row.id + '"  data-rule="required" name="row[type]">' +
                                        '<option value="0"  >请选择</option>' +
                                        '<option value="1"  >星期一</option>' +
                                        '<option value="2"  >星期二</option>' +
                                        '<option value="3" >星期三</option>' +
                                        '<option value="4" >星期四</option>' +
                                        '<option value="5" >星期五</option>' +
                                        '</select>';
                                }

                                // return '<input type="text" class="form-control text-center text-weigh" data-id="' + row.id + '" value="' + value + '" style="width:50px;margin:0 auto;" />';
                            },
                            searchList: {1: __('星期一'), 2: __('星期二'), 3: __('星期三'), 4: __('星期四'), 5: __('星期五')},
                            events: {
                                "dblclick .text-weigh": function (e) {
                                    e.preventDefault();
                                    e.stopPropagation();
                                    return false;
                                }
                            }
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("change", ".text-weigh", function () {
                $(this).data("params", {week: $(this).val()});
                Table.api.multi('', [$(this).data("id")], table, this);
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
